const express = require('express')
const db = require('./db.js')




exports.getlunbotu = (req, res) => {
	let sql = 'select * from shoppinglunbotu'
	// let message = [];
	db.base(sql, null, (results) => {

		// 如果是crud操作,那么result.affectedRows=1 影响行判断成功
		// 如果是查询操作 ,则查询到返回的 result 存在  既判断成功
		if (results) {
			// console.log(results)
			// res.json({ user: 'tobi' });
			res.json(results);
		}


	})
}
exports.getNewsList = (req, res) => {
	let sql = "select * from newsList"
	db.base(sql, null, (results) => {
		if (results) {
			res.json(results)
		} else {
			res.send("查询失败")
		}

	})
}
exports.getnew = (req, res) => {
	let sql = "select * from newsList where id=?";
	let data = [req.params.newid];
	db.base(sql, data, (results) => {
		if (results) {
			// console.l
			res.json(results[0])
		} else {
			res.send("查询失败")
		}

	})

}
exports.getcomments = (req, res) => {

	// 需求 分页查询  0-10  ，10-20 查询数据 ，现在的问题是然后 获取 ？ 后面的值  
	//想法一 ：sql字符串拼接  提前把 这个（0*10，1*10）弄到
	// 获取pageindex的值
	let info = req.query.pageindex; //获取 get  ？后面的参数
	let infopage = '';

	function getlimit(info) {
		if (info == 1) {
			return infopage = 10
		} else {
			infopage = `${(info-1)*10} , ${(info * 10)} `
			return infopage
		}
	}
	getlimit(info);
	// console.log("data的值为"+data);
	let sql = "select * from comments limit " + infopage
	// console.log("sql的值为"+sql);

	let data = [req.params.artid]
	db.base(sql, data, (results) => {
		if (results) {
			res.json(results)
		} else {
			res.send("查询失败")
		}

	})

}
//添加评论
exports.addComments = (req, res) => {
	// console.log('bbbbb')
	let sql = "insert into comments set?"
	// 注意这个body的值的获取  要除了入口要设置vue-resource常规  ， 还需要在用户发送请求的时候 设置 x-www-from-urlcoded请求方式
	let data = req.body

	db.base(sql, data, (result) => {
		if (result.affectedRows = 1) {
			res.json({
				flag: 1
			})

		} else {
			res.json({
				flag: 0
			})
		}
	})

}

//图片分享区
//获取图片列表页面上面的分类信息获取
exports.getimgcategory = (req, res) => {
	let sql = "select * from imgcategory "

	db.base(sql, null, (results) => {
		if (results) {
			res.json(results)
		} else {
			res.send("获取图片列表失败！！")
		}

	})

}

//获取点击首页上的“图片分享”后进入到的列表数据
exports.getimages = (req, res) => {

	let info = req.params.cateid;
	if (info != 0) {
		let sql = "select * from images where groupid=? ";
		let data = [info];
		db.base(sql, data, (results) => {
			if (results) {
				res.json(results)
			} else {
				res.send("获取图片失败！！")
			}
		})

	} else if (info == 0) {
		let sql = "select * from images"
		db.base(sql, null, (results) => {
			if (results) {
				res.json(results)
			} else {
				res.send("获取图片失败！！")
			}

		})

	}



}
//获取图片详情页面中的详细描述信息
exports.getimageInfo = (req, res) => {
	let sql = "select id,title,click,add_time,zhaiyao from images where id=?"
	let data = req.params.imgid
	db.base(sql, data, (results) => {
		if (results) {
			res.json(results)
		} else {
			res.send("获取图片详细描述信息失败！！")
		}
	
	})
	
}
//获取图片详情页面上部的图片列表数据
exports.getthumimages = (req, res) => {
	let sql = "SELECT img_url FROM images WHERE groupid=(SELECT groupid FROM images WHERE id=?) "
	let data = req.params.imgid
	db.base(sql, data, (results) => {
		if (results) {
			
			// 将得到的数据数组中的对象img_url  key值改为src
			// let resultString =JSON.stringify(results).replace('img_url',"src")//只能替换一次 
			let resultString =JSON.stringify(results).replace(/img_url/g, 'src')//替换 字符串中所有的 img_url 为 src
			// console.log(resultString);
			let resultParse = JSON.parse(resultString)
			
			// res.json(results)
			res.json(resultParse)
		} else {
			res.send("获取图片详情页面上部的图片列表数据失败！！")
		}
	
	})
	
}

//获取商品列表展示页面中的所有数据
exports.getgoods = (req , res)=>{
	let info = req.query.pageIndex;
	let infopage = '';
	function getlimit(info) {
		if (info == 1) {
			return infopage = 4
		} else {
			infopage = `${(info-1)*4} , ${(info * 4)} `
			return infopage
		}
	}
	
	getlimit(info);
	let sql = "select * from goods limit " + infopage
	// console.log("sql的值为"+sql);
	db.base(sql, null, (results) => {
		if (results) {
			res.json(results)
		} else {
			res.send("goods查询失败")
		}
	
	})
	
	
}

//获取商品的图文介绍数据
exports.getdesc = (req , res)=>{
	let sql = "select title,zhaiyao from goods where id=?"
	let data = [req.params.id]
	db.base(sql, data, (results) => {
		if (results) {
			res.json(results)
		} else {
			res.send("goodsDesc查询失败")
		}
	
	})
}

exports.getgoodsinfo = (req , res)=>{
	let sql = "select img_url from goods where groupid=?"
	let data = [req.params.id]
	
	db.base(sql, data, (results) => {
		if (results) {
			res.json(results)
		} else {
			res.send("goodsDesc查询失败")
		}
	
	})
	
}
exports.getinfos =(req,res )=>{
	let sql = "SELECT id,title,add_time,goods_no, stock_quantity ,market_price ,sell_price  FROM goods WHERE id=?"
	let data = [req.params.id]
	db.base(sql, data, (results) => {
		if (results) {
			res.json(results)
		} else {
			res.send("商品详情查询失败（getinfos）")
		}
	
	})
}


//

exports.getgoodDesc = (req ,res) =>{
	let sql = "select title,zhaiyao,img_url from goods where id=?"
	let data = [req.params.id]
	db.base(sql, data, (results) => {
		if (results) {
			res.json(results)
		} else {
			res.send("查询失败(getGoodDesc)")
		}
	
	})
	
}


//获取购物车列表数据
exports.getshopcarlist =(req ,res)=>{
	
	
	let data = [req.params.ids];
	let sql ="SELECT cou, id, title, sell_price,img_url FROM goods WHERE id IN ("+data+") "
	// console.log(sql)

	db.base(sql, null, (results) => {
		if (results) {
			res.json(results)
		} else {
			res.send("查询失败(getGoodDesc)")
		}
	
	})
}